using Microsoft.AspNetCore.Mvc;
using MediPlatform.Read.Api.Models;
using MediPlatform.Read.Api.Services;
using MediPlatform.ErrorCode;

namespace MediPlatform.Read.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MedicalOrderWriteController : ControllerBase
    {
        private readonly MedicalOrderService _medicalOrderService;
        private readonly ILogger<MedicalOrderWriteController> _logger;

        public MedicalOrderWriteController(
            MedicalOrderService medicalOrderService,
            ILogger<MedicalOrderWriteController> logger)
        {
            _medicalOrderService = medicalOrderService;
            _logger = logger;
        }

        /// <summary>
        /// 订单发货
        /// </summary>
        /// <param name="request">发货请求</param>
        /// <returns>操作结果</returns>
        [HttpPost("ship")]
        public async Task<ApiResult<object>> ShipOrder([FromBody] ShipOrderRequest request)
        {
            try
            {
                _logger.LogInformation("订单发货，订单ID：{OrderId}，物流公司：{LogisticsCompany}，物流单号：{TrackingNumber}", 
                    request.OrderId, request.LogisticsCompany, request.TrackingNumber);

                var result = await _medicalOrderService.ShipOrderAsync(request.OrderId, request.LogisticsCompany, request.TrackingNumber);

                if (result)
                {
                    return ApiResult<object>.Success(new { message = "订单发货成功" });
                }
                else
                {
                    return ApiResult<object>.Error("订单发货失败，请检查订单状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "订单发货失败，订单ID：{OrderId}", request.OrderId);
                return ApiResult<object>.Error("订单发货失败");
            }
        }

        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="request">确认收货请求</param>
        /// <returns>操作结果</returns>
        [HttpPost("confirm-receipt")]
        public async Task<ApiResult<object>> ConfirmReceipt([FromBody] ConfirmReceiptRequest request)
        {
            try
            {
                _logger.LogInformation("确认收货，订单ID：{OrderId}", request.OrderId);

                var result = await _medicalOrderService.ConfirmReceiptAsync(request.OrderId);

                if (result)
                {
                    return ApiResult<object>.Success(new { message = "确认收货成功" });
                }
                else
                {
                    return ApiResult<object>.Error("确认收货失败，请检查订单状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "确认收货失败，订单ID：{OrderId}", request.OrderId);
                return ApiResult<object>.Error("确认收货失败");
            }
        }

        /// <summary>
        /// 获取物流公司列表
        /// </summary>
        /// <returns>物流公司列表</returns>
        [HttpGet("logistics-companies")]
        public ApiResult<List<LogisticsCompanyInfo>> GetLogisticsCompanies()
        {
            try
            {
                _logger.LogInformation("获取物流公司列表");

                var companies = _medicalOrderService.GetLogisticsCompanies();
                return ApiResult<List<LogisticsCompanyInfo>>.Success(companies);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取物流公司列表失败");
                return ApiResult<List<LogisticsCompanyInfo>>.Error("获取物流公司列表失败");
            }
        }

        /// <summary>
        /// 获取物流追踪详情
        /// </summary>
        /// <param name="orderId">订单ID</param>
        /// <returns>物流追踪详情</returns>
        [HttpGet("logistics-tracking/{orderId}")]
        public ApiResult<LogisticsTrackingDetailResponse> GetLogisticsTrackingDetail(long orderId)
        {
            try
            {
                _logger.LogInformation("获取物流追踪详情，订单ID：{OrderId}", orderId);

                var result = _medicalOrderService.GetLogisticsTrackingDetail(orderId);
                return ApiResult<LogisticsTrackingDetailResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取物流追踪详情失败，订单ID：{OrderId}", orderId);
                return ApiResult<LogisticsTrackingDetailResponse>.Error("获取物流追踪详情失败");
            }
        }
    }
} 